package org.dromara.system.business.learning.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.business.common.PageVo;
import org.dromara.system.business.learning.dto.LearningCourseDto;
import org.dromara.system.business.learning.entity.LearningCourse;
import org.dromara.system.business.learning.mapper.LearningCourseMapper;
import org.dromara.system.business.learning.select.LearningSelect;
import org.dromara.system.business.learning.service.LearningCourseService;
import org.dromara.system.enums.BussinessTableEnum;
import org.dromara.system.service.BusinessFileRelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * (LearningCourse)表服务实现类
 *
 * @author zms
 * @since 2025-02-07 14:42:25
 */
@Service
public class LearningCourseServiceImpl extends ServiceImpl<LearningCourseMapper, LearningCourse> implements LearningCourseService {


    @Autowired
    private BusinessFileRelService businessFileRelService;

    @Override
    public Page<LearningCourse> selectPage(PageVo pageVo, LearningSelect learningSelect) {
        Page<LearningCourse> learningCoursePage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        LambdaQueryWrapper<LearningCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(learningSelect.getName()),LearningCourse::getName,learningSelect.getName());
        wrapper.eq(StringUtils.isNotEmpty(learningSelect.getRealm()),LearningCourse::getRealm,learningSelect.getRealm());
        wrapper.eq(StringUtils.isNotEmpty(learningSelect.getType()),LearningCourse::getType,learningSelect.getType());
        wrapper.between(StringUtils.isNotEmpty(learningSelect.getReleaseTime()), LearningCourse::getReleaseTime,learningSelect.getStartTime(),learningSelect.getEndTime());
        wrapper.orderByDesc(LearningCourse::getCreateTime);

        learningCoursePage = this.page(learningCoursePage, wrapper);
        return learningCoursePage;
    }

    @Override
    public Boolean saveInfo(LearningCourseDto learningCourseDto) {
        LearningCourse learningCourse = new LearningCourse();
        BeanUtils.copyProperties(learningCourseDto, learningCourse);
        learningCourse.setReleaseTime(String.valueOf(LocalDateTime.now()));
        learningCourse.setState("0");
        boolean save = this.save(learningCourse);
        Boolean b = businessFileRelService.fileMesSave(learningCourse.getId(), BussinessTableEnum.LEARNING_COURSE.getValue(), learningCourseDto.getFiles());
        return save && b;
    }

    @Override
    public boolean updateInfo(LearningCourseDto learningCourseDto) {
        LearningCourse learningCourse = new LearningCourse();
        BeanUtils.copyProperties(learningCourseDto, learningCourse);
        if (learningCourse.getId() != null){
            businessFileRelService.fileMesDelete(learningCourse.getId(), BussinessTableEnum.LEARNING_COURSE.getValue());
            businessFileRelService.fileMesSave(learningCourse.getId(), BussinessTableEnum.LEARNING_COURSE.getValue(), learningCourseDto.getFiles());
            return this.updateById(learningCourse);
        }
        return false;
    }
}

